package util;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;

/**
 * Created by Tecnocom on 12/17/2014.
 */
public class Response {
    public ArrayList<Object> obj;
    private String result;
    private String strBody;
    private JSONObject jsonBody;
    private Boolean existBody;
    private Integer response;
    private String message;

    // Crear Obtejo Vacío
    public Response(){
        this.result = null;
        this.response = null;
        this.message = null;
        this.obj = null;
    }

    // Crear Obtejo respuesta
    public Response(String result){
        this.result = result;
        Log.i("Trace 'new Response' result", result);

        this.response = getDataJsonInt("header", "response");
        this.message = getDataJsonStr("header", "message");

        this.jsonBody = new JSONObject();
        this.jsonBody = getJsonBody();

        // Se recibio body en la respuesta
        if (this.jsonBody != null){
            this.existBody = true;
            this.strBody = this.jsonBody.toString();
            Log.i("Trace 'new Response', jsonBody: ", this.jsonBody.toString());
        }else{
            this.existBody = false;
            this.strBody = "<NULL>";
            Log.i("Trace 'new Response': ", "NO se recibio Body en la respuesta");
        }
    }

    public String getResultStr(){
        return this.result;
    }

    public String getBodyStr(){
        return this.strBody;
    }

    public Integer getHeaderResponse() {
        return this.response;
    }

    public String getHeaderMessage() {
        return this.message;
    }

    public String getBodyValueStr(String field){
        String res = "<NULL>";
        if (existBody)
            res = getDataJsonStr("body", field);
        return res;
    }

    public Integer getBodyValueInt(String field){
        Integer res = -999;
        if (existBody)
            res = getDataJsonInt("body", field);
        return res;
    }

    public int getListLength(String list){
        int res = 0;
        JSONArray jArray = null;
        if (existBody) {
            try {
                jArray = jsonBody.getJSONArray(list);
                res = jArray.length();
            } catch (JSONException e) {
                e.printStackTrace();
                Log.i("Parse Exception 'getListLength': ", e.toString());
            }
        }
        return res;
    }

    public String getBodyValueArrayStr(String list, Integer idx, String field){
        String res = "<NULL>";
        JSONArray jArray = null;
        JSONObject r = null;
        Integer len;
        if (existBody) {
            try {
                jArray = jsonBody.getJSONArray(list);
                len = jArray.length();
                // si el indice requerido supera la longitud del array se retorna null
                if (idx > len - 1) {
                    res = "<NULL>";
                    Log.i("Parse Exception 'getBodyValueArrayStr': ", "Indice mayor a la longitud del array");
                } else {
                    r = jArray.getJSONObject(idx);
                    res = r.getString(field);
                    //Log.i("Trace 'getBodyValueArrayStr', res: ", res);
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.i("Parse Exception 'getJsonListBody': ", e.toString());
            }
        }
        return res;
    }

    public Integer getBodyValueArrayInt(String list, Integer idx, String field){
        Integer res = -999;
        JSONArray jArray = null;
        JSONObject r = null;
        Integer len;
        if (existBody) {
            try {
                jArray = jsonBody.getJSONArray(list);
                len = jArray.length();
                // si el indice requerido supera la longitud del array se retorna null
                if (idx > len - 1) {
                    res = -999;
                    Log.i("Parse Exception 'getBodyValueArrayInt': ", "Indice mayor a la longitud del array");
                } else {
                    r = jArray.getJSONObject(idx);
                    res = r.getInt(field);
                    Log.i("Trace 'getBodyValueArrayInt', res: ", ""+res);
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.i("Parse Exception 'getJsonListBody': ", e.toString());
            }
        }
        return res;
    }

    public Double getBodyValueArrayDouble(String list, Integer idx, String field){
        Double res = -999d;
        JSONArray jArray = null;
        JSONObject r = null;
        Integer len;
        if (existBody) {
            try {
                jArray = jsonBody.getJSONArray(list);
                len = jArray.length();
                // si el indice requerido supera la longitud del array se retorna null
                if (idx > len - 1) {
                    res = -999d;
                    Log.i("Parse Exception 'getBodyValueArrayInt': ", "Indice mayor a la longitud del array");
                } else {
                    r = jArray.getJSONObject(idx);
                    res = r.getDouble(field);
                    Log.i("Trace 'getBodyValueArrayInt', res: ", ""+res);
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.i("Parse Exception 'getJsonListBody': ", e.toString());
            }
        }
        return res;
    }

    public JSONObject getJsonBody() {
        String res = "KO";
        JSONObject json = null;
        JSONObject json1 = null;
        try {
            json = new JSONObject(this.result);
            Log.i("Trace 'getJsonBody', json.toString: ", json.toString());

            json1 = json.getJSONObject("body");
            Log.i("Trace 'getJsonBody', json1: ", json1.toString());

        } catch (Exception e) {
            e.printStackTrace();
            Log.i("Parse Exception 'getJsonBody': ", e.toString());
        }
        return json1;
    }

    public String getDataJsonStr(String parent, String son) {
        String res = "<NULL>";
        try {
            JSONObject json= (JSONObject) new JSONTokener(this.result).nextValue();
            JSONObject json2 = json.getJSONObject(parent);
            res = json2.getString(son);
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("Parse Exception 'getDataJson': ", e.toString());
        }
        return res;
    }

    public Integer getDataJsonInt(String parent, String son) {
        Integer res = -999;
        try {
            JSONObject json= (JSONObject) new JSONTokener(this.result).nextValue();
            JSONObject json2 = json.getJSONObject(parent);
            res = json2.getInt(son);
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("Parse Exception 'getDataJson': ", e.toString());
        }
        return res;
    }
}

